//
//  UIColor+Util.m
//  ifangchou
//
//  Created by guang on 13-12-30.
//  Copyright (c) 2013年 ifangchou. All rights reserved.
//

#import "UIColor+Util.h"

@implementation UIColor (Util)

+(UIColor *)getColor:(NSString *)hexColor
{
    unsigned int red, green, blue;
    NSRange range;
    range.length = 2;
    
    range.location = 0;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&red];
    range.location = 2;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&green];
    range.location = 4;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&blue];
    
    return [UIColor colorWithRed:(float)(red/255.0f)green:(float)(green/255.0f)blue:(float)(blue/255.0f)alpha:1.0f];
}

+(UIColor*)colorWithString:(NSString *)colorString{
    
    if (colorString == nil)
    {
        return nil;
    }
    NSArray *colorStringArray = [colorString componentsSeparatedByString:@" "];
    if (colorStringArray.count < 3)
    {
        return nil;
    }
    int redColor = [[colorStringArray objectAtIndex:0] intValue];
    int greenColor = [[colorStringArray objectAtIndex:1] intValue];
    int blueColor = [[colorStringArray objectAtIndex:2] intValue];
    int alpha = 255;
    if (colorStringArray.count >= 4)
    {
        alpha = [[colorStringArray objectAtIndex:3] intValue];
    }
    return [UIColor colorWithRed:redColor/255.0 green:greenColor/255.0 blue:blueColor/255.0 alpha:alpha/255.0];
}


@end
